package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.BizPandianLog;
import com.ruoyi.system.domain.BizZichan;
import com.ruoyi.system.service.IBizPandianLogService;
import com.ruoyi.system.service.IBizZichanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BizPandian;
import com.ruoyi.system.service.IBizPandianService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资产清查Controller
 * 
 * @author ruoyi
 * @date 2024-12-16
 */
@Controller
@RequestMapping("/system/pandian")
public class BizPandianController extends BaseController
{
    private String prefix = "system/pandian";

    @Autowired
    private IBizPandianService bizPandianService;

    @Autowired
    private IBizZichanService bizZichanService;

    @Autowired
    private IBizPandianLogService bizPandianLogService;

    @RequiresPermissions("system:pandian:view")
    @GetMapping()
    public String pandian()
    {
        return prefix + "/pandian";
    }

    /**
     * 查询资产清查列表
     */
    @RequiresPermissions("system:pandian:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizPandian bizPandian)
    {
        startPage();
        List<BizPandian> list = bizPandianService.selectBizPandianList(bizPandian);
        return getDataTable(list);
    }

    /**
     * 导出资产清查列表
     */
    @RequiresPermissions("system:pandian:export")
    @Log(title = "资产清查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizPandian bizPandian)
    {
        List<BizPandian> list = bizPandianService.selectBizPandianList(bizPandian);
        ExcelUtil<BizPandian> util = new ExcelUtil<BizPandian>(BizPandian.class);
        return util.exportExcel(list, "资产清查数据");
    }

    /**
     * 新增资产清查
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资产清查
     */
    @RequiresPermissions("system:pandian:add")
    @Log(title = "资产清查", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizPandian bizPandian)
    {
        return toAjax(bizPandianService.insertBizPandian(bizPandian));
    }

    /**
     * 修改资产清查
     */
    @RequiresPermissions("system:pandian:edit")
    @GetMapping("/edit/{zichanbianhao}")
    public String edit(@PathVariable("zichanbianhao") String zichanbianhao, ModelMap mmap)
    {
        BizPandian bizPandian = bizPandianService.selectBizPandianByZichanbianhao(zichanbianhao);
        mmap.put("bizPandian", bizPandian);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产清查
     */
    @RequiresPermissions("system:pandian:edit")
    @Log(title = "资产清查", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizPandian bizPandian)
    {
        BizZichan zichan=bizZichanService.selectBizZichanByBianhao(bizPandian.getZichanbianhao());
        String shuoming="";
        if (bizPandian.getPandianzhuangtai()==3){
            //地点变更
            shuoming="移动";
            bizPandian.setTijiaozhuangtai(1L);
        }else if(bizPandian.getPandianzhuangtai()==9){
            //报废
            shuoming="待处置";
            bizPandian.setTijiaozhuangtai(1L);
        }else if(bizPandian.getPandianzhuangtai()==2){
            shuoming="清查中";
        }else if(bizPandian.getPandianzhuangtai()==1){
            shuoming="已清查";
            bizPandian.setTijiaozhuangtai(1L);
        }else if(bizPandian.getPandianzhuangtai()==0){
            shuoming="未清查";
        }

        //插入日志表
        BizPandianLog bizPandianLog = new BizPandianLog();
        bizPandianLog.setZichanbianhao(bizPandian.getZichanbianhao());
        bizPandianLog.setZichanmingcheng(bizPandian.getZichanmingcheng());
        bizPandianLog.setTijiaoshijian(new Date());
        bizPandianLog.setCunfangdidianNew(bizPandian.getCunfangdidian());
        bizPandianLog.setCunfangdidianOld(zichan.getCunfangdidian());
        bizPandianLog.setShuoming(shuoming);
        bizPandianLogService.insertBizPandianLog(bizPandianLog);
        //更新资产表
        if (bizPandian.getPandianzhuangtai()==3){
            //地点变更
            BizZichan bizZichan=new BizZichan();
            bizZichan.setZichanbianhao(bizPandian.getZichanbianhao());
            bizZichan.setCunfangdidian(bizPandian.getCunfangdidian());
            bizZichanService.updateBizZichanDidian(bizZichan);
        }else if(bizPandian.getPandianzhuangtai()==9){
            //报废
            BizZichan bizZichan=new BizZichan();
            bizZichan.setZichanbianhao(bizPandian.getZichanbianhao());
            bizZichan.setZichanzhuangtai("待处置");
            bizZichanService.updateBizZichanDidian(bizZichan);
        }

        //更新盘点表
        return toAjax(bizPandianService.updateBizPandian(bizPandian));
    }

    /**
     * 删除资产清查
     */
    @RequiresPermissions("system:pandian:remove")
    @Log(title = "资产清查", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizPandianService.deleteBizPandianByZichanbianhaos(ids));
    }


    @GetMapping("/huizong")
    @ResponseBody
    public AjaxResult huizong()
    {
        return AjaxResult.success(bizPandianService.selectPandianHuizong());
    }
}
